--[[控制canvas是否绘制
1.canvas显示的条件。
2.canvas显示的次序。
3.canvas不显示。
]]
var = 10   -- arbitrary starting value
rate = 3   -- change to change the rate at which the var is changed

function love.update(dt)
      var = var + (dt * rate)
end

function love.load()
    love.graphics.setBackgroundColor(1,1,1)
    --画布的显示次序，用一个Table记录
    canvas_z_index={"canvas1","canvas2","canvas3"}
    --画布的属性
    canvas1property ={posx=50,posy=50,width=100,height=200,visible=false}
    canvas2property ={posx=100,posy=100,width=100,height=200,visible=false}
    canvas3property ={posx=150,posy=150,width=100,height=200,visible=false}
    canvas1button1property={posx=50,posy=50,width=100,height=200,visible=false}

    --新建了画布0
    canvas0 = love.graphics.newCanvas(800, 600)
    love.graphics.setCanvas(canvas0)
        love.graphics.clear()
        love.graphics.setColor(1, 0, 0)
        love.graphics.rectangle('fill', 0, 0, 100, 200)
        love.graphics.setColor(0, 0, 0)
        love.graphics.rectangle('fill', 0, 0, 100, 20) --标题栏
        love.graphics.setColor(1, 1, 1)
        love.graphics.print("canvas0",10,2)
        --绘制图片
        --love.graphics.setBackgroundColor(1,1,1)
        image1 = love.graphics.newImage( "resources/images/scenario/default/persons/F01.png" )
        love.graphics.draw(image1,30,30)
    love.graphics.setCanvas()    
    --新建了画布1
    canvas1 = love.graphics.newCanvas(800, 600)
    love.graphics.setCanvas(canvas1)
        love.graphics.clear()
        love.graphics.setColor(1, 0, 0)
        love.graphics.rectangle('fill', 0, 0, 100, 200)
        love.graphics.setColor(0, 0, 0)
        love.graphics.rectangle('fill', 0, 0, 100, 20) --标题栏
        love.graphics.rectangle('fill', 0, 180, 100, 20) --关闭
        love.graphics.setColor(1, 1, 1)
        love.graphics.print("canvas1",10,2)
        love.graphics.print("close me",10,182)
        --绘制图片
        --love.graphics.setBackgroundColor(1,1,1)
        image1 = love.graphics.newImage( "resources/images/scenario/default/persons/F01.png" )
        love.graphics.draw(image1,30,30)
    love.graphics.setCanvas()
    --新建了画布2
    canvas2 = love.graphics.newCanvas(800, 600)
    love.graphics.setCanvas(canvas2)
        love.graphics.clear()
        love.graphics.setColor(0, 1, 0)
        love.graphics.rectangle('fill', 0, 0, 100, 200)
        love.graphics.setColor(0, 0, 0)
        love.graphics.rectangle('fill', 0, 0, 100, 20) --标题栏
        love.graphics.setColor(1, 1, 1)
        love.graphics.print("canvas2",10,2)
    love.graphics.setCanvas()
   --新建了画布3
   canvas3 = love.graphics.newCanvas(800, 600)
   love.graphics.setCanvas(canvas3)
       love.graphics.clear()
       love.graphics.setColor(0, 0, 1)
       love.graphics.rectangle('fill', 0, 0, 100, 200)
       love.graphics.setColor(0, 0, 0)
       love.graphics.rectangle('fill', 0, 0, 100, 20) --标题栏
       love.graphics.setColor(1, 1, 1)
       love.graphics.print("canvas3",10,2)
   love.graphics.setCanvas()

end
 
function love.draw()
    --middleclass测试区
    local class = require 'libraries.middleclass.middleclass'
    local Fruit = class('Fruit') -- 'Fruit' is the class' name   
    function Fruit:initialize(sweetness)
      self.sweetness = sweetness
    end    
    Fruit.static.sweetness_threshold = 5 -- class variable (also admits methods)   
    function Fruit:isSweet()
      return self.sweetness > Fruit.sweetness_threshold
    end    
    local Lemon = class('Lemon', Fruit) -- subclassing   
    function Lemon:initialize()
      Fruit.initialize(self, 1) -- invoking the superclass' initializer
    end    
    local lemon = Lemon:new() 
    love.graphics.setColor(0, 0, 0)
    love.graphics.print(tostring(lemon:isSweet()),600,20)
    --middleclass测试区
    --graphics直接画按钮
    love.graphics.setColor(192/255, 192/255, 192/255)
    love.graphics.rectangle('fill', 0, 0, 100, 100)  
    love.graphics.rectangle('fill', 120, 0, 100, 100) 
    love.graphics.rectangle('fill', 240, 0, 100, 100)  
    love.graphics.setColor(1, 1, 1)
    love.graphics.print("button1\ncanvas1",10,10)
    love.graphics.print("button2\ncanvas2",130,10)
    love.graphics.print("button3\ncanvas3",250,10)
    love.graphics.setColor(0, 0, 0)
    love.graphics.print(tostring(canvas_z_index[3]),10,110)   
    love.graphics.print(tostring(canvas_z_index[2]),10,150)   
    love.graphics.print(tostring(canvas_z_index[1]),10,190)   
    love.graphics.print("canvas1.visible="..tostring(canvas1property.visible),10,230)   
    love.graphics.print("canvas2.visible="..tostring(canvas2property.visible),10,270)   
    love.graphics.print("canvas3.visible="..tostring(canvas3property.visible),10,310)   
    
    love.graphics.setColor(1, 1, 1)
    local x, y = love.mouse.getPosition() -- get the position of the mouse
    i=1
    ::s0::  
    if canvas_z_index[i]=="canvas1" then
        i=i+1
        goto s1
    elseif canvas_z_index[i]=="canvas2"  then
        i=i+1       
        goto s2
    elseif canvas_z_index[i]=="canvas3"  then
        i=i+1       
        goto s3
    else
        goto s4
    end

    ::s1:: 
    if canvas1property.visible == true then
        love.graphics.draw(canvas1, canvas1property.posx, canvas1property.posy)    
    end
    goto s0
    ::s2::
    if canvas2property.visible == true then
        love.graphics.draw(canvas2, canvas2property.posx, canvas2property.posy)
    end
    goto s0
    ::s3::
    if canvas3property.visible == true then
        love.graphics.draw(canvas3, canvas3property.posx, canvas3property.posy)
    end
    goto s0
    ::s4::
end

function love.keypressed(key)
    if key == "escape" then
       love.event.quit()
    end
    if love.keyboard.isDown("down") then   -- reduce the value
       var = var -  rate
    end
    if love.keyboard.isDown("up") then   -- increase the value
       var = var + rate
    end   
 end

function love.mousepressed(x, y, button, istouch)
    local moux= love.mouse.getX( )
    local mouy=love.mouse.getY( )
    if moux>0 and moux <100 and mousepressedcanvas=="graphics" then
        if mouy>0 and mouy<100 then
            if love.mouse.isDown(1) then
                for i=1,3,1 do
                   if  canvas_z_index[i]=="canvas1" then    
                   table.remove(canvas_z_index, i)
                   end
                end
                table.insert(canvas_z_index, 3, "canvas1")
                canvas1property.visible=not(canvas1property.visible)  
            end
        end
    end
    if moux>120 and moux <220 and mousepressedcanvas=="graphics" then
        if mouy>0 and mouy<100 then
            if love.mouse.isDown(1) then
                for i=1,3,1 do
                    if  canvas_z_index[i]=="canvas2" then    
                    table.remove(canvas_z_index, i)
                    end
                 end
                 table.insert(canvas_z_index, 3, "canvas2")
                canvas2property.visible=not(canvas2property.visible)  
            end
        end
    end
    if moux>240 and moux <340 and mousepressedcanvas=="graphics" then
        if mouy>0 and mouy<100 then
            if love.mouse.isDown(1) then
                for i=1,3,1 do
                    if  canvas_z_index[i]=="canvas3" then    
                    table.remove(canvas_z_index, i)
                    end
                 end
                 table.insert(canvas_z_index, 3, "canvas3")
                canvas3property.visible=not(canvas3property.visible)  
            end
        end
    end     
end
function love.mousemoved( x, y, dx, dy, istouch)    --鼠标捕捉
    local moux= love.mouse.getX( )
    local mouy=love.mouse.getY( )
    mousepressedcanvas="graphics" 
    --canvas1的移动
    if moux>canvas1property.posx and moux <canvas1property.posx+canvas1property.width then
        if mouy>canvas1property.posy and mouy<canvas1property.posy+canvas1property.height then
            if canvas1property.visible  then mousepressedcanvas="canvas1" end
            if love.mouse.isDown(1) and canvas1property.visible then             
             canvas1property.posx = canvas1property.posx+dx
             canvas1property.posy = canvas1property.posy+dy        
            end
        end
    end
    --canvas2的移动
    if moux>canvas2property.posx and moux <canvas2property.posx+canvas2property.width then
        if mouy>canvas2property.posy and mouy<canvas2property.posy+canvas2property.height then
            if canvas2property.visible  then mousepressedcanvas="canvas2" end--判断鼠标按的是哪一层canvas
             if love.mouse.isDown(1) and canvas2property.visible then
             canvas2property.posx = canvas2property.posx+dx
             canvas2property.posy = canvas2property.posy+dy        
            end
        end
    end
    --canvas3的移动
    if moux>canvas3property.posx and moux <canvas3property.posx+canvas3property.width then
        if mouy>canvas3property.posy and mouy<canvas3property.posy+20 then
            if canvas3property.visible  then mousepressedcanvas="canvas3" end--判断鼠标按的是哪一层canvas
             if love.mouse.isDown(1) and canvas3property.visible then
             canvas3property.posx = canvas3property.posx+dx
             canvas3property.posy = canvas3property.posy+dy        
            end
        end
    end
end  